<?php
namespace Kxdrmobile\Model;
use Think\Model;
// 该类save方法出错，save用M新建类,add也出错，为什么呢,$myfields 不能被重定义
class UserModel extends Model{
	public $mixednick;
	public $sid;
	public $id;
	public $myfields="`id`,`join_time`,`avatar`,`sm_uid`,`shop_uid`,`nengli`";
	public $user_info;
	public $user_score;
	public function getUser(){
		$data["sid"] = $this->sid;
		$data["mixed_nick"] = $this->mixednick;
		$friend = current($this->where("`sid`=%d AND `mixed_nick`='%s'",$data)->getField($this->myfields));

		if(!$friend){
			$id = $this->addUser();
		}else{
			$id = $friend["id"];
			$friend["sid"]=$this->sid;
			$friend["mixed_nick"] = $this->mixednick;
			$friend["avatar"] = $friend["avatar"]?$friend["avatar"]:"";
			$this->user_info = $friend;
		}
		return $id;
	}
	public function getUserFields(){
		$data["id"] = $this->uid;
		$data["sid"] = $this->sid;
		$friend = current($this->cache(true,60)->where("`id`=".$this->uid." AND `sid`=".$this->sid)->getField($this->myfields));
		$this->user_info = $friend;
		return $friend;
	}
	public function getUserField(){
		$data["id"] = $this->uid;
		$data["sid"] = $this->sid;
		return $this->where("`id`=".$this->uid." AND `sid`=".$this->sid)->getField($this->myfields);
	}
	public function incJoinEvents(){
		$this->setInc("joined_events");
	}
	public function addUser(){
		$SMU = D("MainUser");
		$SPU = D("ShopUsers");
		$smu["mixednick"] = $this->mixednick;
		$data["sm_uid"] = $SMU->where($smu)->getField("id");

		if(!$data["sm_uid"]){
			$data["sm_uid"] = $SMU->add($smu);
			
			$spu["sid"] = $this->sid;
			$spu["userid"] = $data["sm_uid"];
			$data["shop_uid"]=$SPU->add($spu);
		}
		if(!$data["shop_uid"]){
			$data["shop_uid"]=$SPU->where("`userid`=".$data["sm_uid"])->getField("`id`");
		}
		
		$data["avatar"] = $this->getAvatar();
		$data["nengli"] = 0;
		$data["friends"] = 0;
		$data["mixed_nick"] = $this->mixednick;
		$data["sid"] = $this->sid;
		$data["join_time"] = date("Y-m-d H:i:s");
		$data["friend_link"] = "/?friendnick=".$this->mixednick."&sid=".$this->sid;
		$data["id"] = $this->add($data);
		$this->user_info = $data;
		return $data["id"];
	}
	public function getAvatar(){		
			Vendor("taobaosdk.top.TopClient");
			Vendor('taobaosdk.lotusphp_runtime.Logger.Logger');
			Vendor("taobaosdk.top.RequestCheckUtil");
			Vendor("taobaosdk.top.request.UserAvatarGetRequest");
			$c = new \TopClient;
			$c->appkey = C("SM_KEY");//用后台的key
			$c->secretKey = C("SM_SECRET"); 
			$req = new \UserAvatarGetRequest;
			$req->setNick($this->mixednick);
			$resp = $c->execute($req);
			$this->avatar = (string)$resp->avatar;
		return $this->avatar;
	}

	public function getUserJifen(){
		$JF = D("JifenUserView");
		return $JF->where("`kxdr_user`.`sid`=".$this->sid." AND `kxdr_user`.`id`=".$this->uid)->getField("seeds");
	}
	public function friendsUp(){
		$where["id"]=$this->uid;
		$where["sid"]=$this->sid;
		return $this->where($where)->setInc("friends");
	}
	public function incJifen($jifen){
		$JF = D("JifenUserView");
		return $JF->where("`kxdr_user`.`sid`=".$this->sid." AND `kxdr_user`.`id`=".$this->uid)->setInc("seeds",$jifen);
	}
	public function decJifen($jifen){
		$JF = D("JifenUserView");
		return $JF->where("`kxdr_user`.`sid`=".$this->sid." AND `kxdr_user`.`id`=".$this->uid)->setDec("seeds",$jifen);
	}
	
	public function getUserTitle(){
		
		$UT = D("UserTitleView");
		$wt["sid"]=$this->sid;
		$wt["user_id"]=$this->uid;
		$wt["score"]= array('egt',$this->user_score);
		$user_title = $UT->where($wt)->limit(1)->getField("title");

		if(!$user_title){
			$ST = M("HonoraryTitle");
			$st = $ST->where("`sid`=".$this->sid." AND `score`>=".$this->user_score)->limit(1)->getField("`id`,`title`");
			if($st){
				$tt["sid"]=$this->sid;
				$tt["user_id"]=$this->uid;
				$tt["honor_id"]=key($st);
				$tt["honor_title"] = $user_title = current($st);
				$tt["add_time"]=date("Y-m-d H:i:s");
				$TT = M("HonoraryTitleUser");
				$TT->add($tt);
			}
		}
		
		return $user_title;
	}
}